﻿//  -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	Smart.Common
//  File name:	FormsRuleChecker.cs
//  Developer:	Solomatov Igor
//  Created:	15.04.2012
// 
//  -------------------- iSharePoint -------------------- //

using System;
using Smart.Common.Rules;

namespace Smart.Common.Forms
{
	public class FormsRuleChecker : XmlRuleChecker
	{
		public FormsRuleChecker(RulesCtx ctx)
			: base(ctx)
		{
		}

		public override bool ExecuteFunction(XmlRuleConditionFunction function, out bool handled)
		{
			// Check function in base classes
			var res = base.ExecuteFunction(function, out handled);

			if (handled)
				return res;

			// Check choose rule function
			var cri = Ctx.Instance as FormsChooseRulesInstance;

			if (cri != null)
			{
				handled = true;
				return cri.Ctx.Service.Builder.ExecuteFunction(function.Name, function.Parameters);
			}

			// Check action rule function
			var ari = Ctx.Instance as FormsActionRulesInstance;

			if (ari != null)
			{
				handled = true;
				return ari.Ctx.Service.Formatter.ExecuteFunction(function.Name, function.Parameters);
			}

			throw new NotSupportedException(function.Name);
		}
	}
}